package org.centsuse.intention_spring.invention.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.centsuse.intention_spring.invention.config.scenes.Parameter;
import org.centsuse.intention_spring.invention.config.scenes.SceneSlotTemplate;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 提示词工具类
 */
public class PromptUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper()
            .enable(SerializationFeature.INDENT_OUTPUT)
            .disable(JsonWriteFeature.ESCAPE_NON_ASCII.mappedFeature());

    private static final DateTimeFormatter DATE_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd");

    // 场景提示语配置（可从配置文件加载）TODO
    public static class ScenePrompts {
        public static final String SLOT_UPDATE =
                "场景名称：%s\n当前日期：%s\n参数列表：%s\n示例响应：%s\n当前参数：%s\n用户输入：%s\n注意：只返回json格式";

        public static final String SLOT_QUERY_USER =
                "场景名称：%s\n缺失参数：%s\n用户输入：%s";
    }

    // 槽位更新消息生成

    /**
     * @description 获取参数提取信息提示词
     * @param sceneName 场景名称
     * @param parameters 场景参数列表
     * @param dynamicExample 场景示例
     * @param slotTemplate 当前参数
     * @param userInput 用户输入
     * @return 拼装后的提示词
     * @throws JsonProcessingException
     */
    public static String getSlotUpdateMessage(String sceneName,
                                              Collection<Parameter> parameters,
                                              String dynamicExample,
                                              Collection<SceneSlotTemplate> slotTemplate,
                                              String userInput)
            throws JsonProcessingException {

        String currentDate = LocalDate.now().format(DATE_FORMATTER);
        String slotJson = toJson(getSlotUpdateJson(slotTemplate));

        return String.format(ScenePrompts.SLOT_UPDATE,
                sceneName,
                currentDate,
                parameters,
                dynamicExample,
                slotJson,
                userInput
        );
    }

    // 用户查询消息生成
    public static String getSlotQueryUserMessage(String sceneName,
                                                 Collection<SceneSlotTemplate> slot,
                                                 String userInput)
            throws JsonProcessingException {

        String slotJson = toJson(getSlotQueryUserJson(slot));
        return String.format(ScenePrompts.SLOT_QUERY_USER,
                sceneName,
                slotJson,
                userInput
        );
    }

    // 槽位转换方法（与之前代码保持兼容）
    private static List<SceneSlotTemplate> getSlotUpdateJson(Collection<SceneSlotTemplate> slotTemplate) {
        return slotTemplate.stream()
                .map(s -> new SceneSlotTemplate(s.getName(), s.getDesc(), s.getType(), s.getValue()))
                .collect(Collectors.toList());
    }

    private static List<SceneSlotTemplate> getSlotQueryUserJson(Collection<SceneSlotTemplate> slots) {
        return slots.stream()
                .filter(s -> null == s.getValue())
                .collect(Collectors.toList());
    }

    // 通用 JSON 序列化方法
    private static String toJson(Object obj) throws JsonProcessingException {
        return objectMapper.writeValueAsString(obj);
    }
}